package com.orange.lockscreen.layers.finger;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.orange.lockscreen.R;
import com.orange.lockscreen.SlatHashMap;
import com.orange.lockscreen.common.LockScreenPreferences;
import com.orange.lockscreen.layers.SlatPanel;

/**
 * 
 * SwipePanel: show double circle animation around main unlock icon.
 * 
 */
public class SwipePanel extends SlatPanel {

	private static final int	TOTAL_DISAPPEAR	= 1;
	private static final int	TIME_DISAPPEAR	= 800;

	private ImageView			mFirstCircle;
	private ImageView			mSecondCircle;
	private CircleSlat			mSwipeSlat;

	private H					mH;
	private Animation			mAlphaAnimation1;
	private Animation			mAlphaAnimation2;

	class H extends Handler {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TOTAL_DISAPPEAR:
				break;
			default:
				break;
			}
		}
	}

	public SwipePanel(Context context) {
		super(context);
	}

	@Override
	protected void init() {
		mH = new H();

		mSwipeSlat = new CircleSlat(getContext());
		mSwipeSlat.setClipChildren(true);
		addView(mSwipeSlat);

		mFirstCircle = mSwipeSlat.getFirstCircleView();
		mSecondCircle = mSwipeSlat.getSecondCircleView();

		mAlphaAnimation1 = AnimationUtils.loadAnimation(getContext(),
				R.anim.swipe_anim);
		mAlphaAnimation2 = AnimationUtils.loadAnimation(getContext(),
				R.anim.swipe_anim);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int top = SlatHashMap.getSlatTop(SlatHashMap.UNLOCK_ROWINDEX);
		int rowHeight = SlatHashMap.getSlatHeight(SlatHashMap.UNLOCK_ROWINDEX);
		mSwipeSlat.layout(0, top, r - l, top + rowHeight);
	}

	public void show() {
		boolean isAnimating = mH.hasMessages(TOTAL_DISAPPEAR);
		if (isAnimating) {
			mFirstCircle.clearAnimation();
			mSecondCircle.clearAnimation();
			mFirstCircle.setVisibility(INVISIBLE);
			mSecondCircle.setVisibility(INVISIBLE);
			mH.removeMessages(TOTAL_DISAPPEAR);
		}
		setCirclePosition();
		mFirstCircle.requestLayout();
		mFirstCircle.setVisibility(VISIBLE);
		mFirstCircle.startAnimation(mAlphaAnimation1);
		mSecondCircle.postDelayed(new Runnable() {
			@Override
			public void run() {
				mSecondCircle.setVisibility(VISIBLE);
				mSecondCircle.startAnimation(mAlphaAnimation2);
			}
		}, 200);
		mH.sendEmptyMessageDelayed(TOTAL_DISAPPEAR, TIME_DISAPPEAR);
	}

	private void setCirclePosition() {
//		int x = (int) (getWidth() / 2.0);
		int x = (int) (SlatHashMap.getScreenWidth() / 2.0);
		int y = 0;
		if (LockScreenPreferences.isUnlockPromptEnabled()) {
			y = (int) getContext().getResources().getDimension(
					R.dimen.prompt_enable_circle_topoffset);
		} else {
			y = (int) getContext().getResources().getDimension(
					R.dimen.prompt_disable_circle_topoffset);
		}
		mSwipeSlat.setCirclePosition(x, y);
	}
}
